﻿using 通用类库.DataProvider;
using 通用类库.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace 通用类库.Stock
{
    public class StockCache                                        //public是公有的，class是类型，StockCache是股票缓存
    {
        private static StockCache _instance = null;     //null是无类型指针
        public static StockCache Instance                    //构造方法，完成对象初始化，static是静态的
        {
            get
            {
                if (_instance == null)
                    _instance = new StockCache();
                return _instance;
            }
        }
        private StockCache()                                             //private是私有的
        {
            initData();
        }

        private void initData()
        {
            if (_dicCode2Price.Count <= 0)
            {
                //先从本地文件读取
                string filePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "StockData");
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);
                string fileName = Path.Combine(filePath, "Stock.dat");
                if (!File.Exists(fileName))                                                      //如果文件不存在。
                {
                    //从通达信获取数据
                    List<StockLevel2Info> allInfo = GlobalInstanceCache.Instance.DataInstance.GetAllMarketStock();
                    string[] strValue = new string[allInfo.Count];

                    for (int i = 0; i < allInfo.Count; i++)
                    {
                        strValue[i] = string.Format("{0},{1}", allInfo[i].StockCode, allInfo[i].StockName);     //使用了占位符     {0}股票代码     {1}股票名称
                        _dicCode2Price[allInfo[i].StockCode] = allInfo[i];
                    }
                    File.WriteAllLines(fileName, strValue);
                }
                else
                {
                    string[] allStock = File.ReadAllLines(fileName);
                    for (int i = 0; i < allStock.Length; i++)
                    {
                        StockLevel2Info tmpInfo = new StockLevel2Info();
                        string[] strArray = allStock[i].Split(new char[] { ',' });
                        if (strArray.Length >= 2)
                        {
                            tmpInfo.StockCode = strArray[0];
                            tmpInfo.StockName = strArray[1];
                            _dicCode2Price[tmpInfo.StockCode] = tmpInfo;
                        }
                    }
                }
            }
        }

        public StockLevel2Info GetStockPrice(string stockCode)
        {
            return GlobalInstanceCache.Instance.DataInstance.GetStockPrice(stockCode);    //return是返回值
        }

        private Dictionary<string, StockLevel2Info> _dicCode2Price = new Dictionary<string, StockLevel2Info>();

        public string GetStockName(string stockCode)
        {
            if (_dicCode2Price.ContainsKey(stockCode))
                return _dicCode2Price[stockCode].StockName;
            return string.Empty;
        }
    }
}
